package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;

import com.liaoye.hellocustomview.util.DPUtil;

public class PieView extends View {

    private int radius = 0;
    private Paint mPaint;
    private RectF mRectF;

    private int[] angles = new int[]{30, 60, 90, 180};
    private int[] colors = new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};

    private int pieOutIndex = 1;

    {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(DPUtil.dpToPix(2));
        mRectF = new RectF();
//        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    }

    public PieView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();
        int centerX = width / 2;
        int centerY = height / 2;
        radius = width / 3;

        mRectF.set(centerX - radius, centerY - radius, centerX + radius, centerY + radius);

        int startAngle = 60;
        for (int i = 0; i < angles.length; i++) {
            int sweepAngle = angles[i];
            canvas.save();
            if(i == pieOutIndex) {
                float dx = Double.valueOf(Math.cos(Math.toRadians(startAngle + sweepAngle / 2)) * 20).floatValue();
                float dy = Double.valueOf(Math.sin(Math.toRadians(startAngle + sweepAngle / 2)) * 20).floatValue();
                Log.i("PieView", "onDraw: " + dx + " == " + dy);
                canvas.translate(dx, dy);
            }
            mPaint.setColor(colors[i]);
            canvas.drawArc(mRectF, startAngle, sweepAngle, true, mPaint);
            canvas.restore();
            startAngle = startAngle + sweepAngle;
        }
    }
}
